package com.wgts.wgts.mvp.view.activity.tasklobby;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.dialog.WonderLoadingDialog;
import com.wgts.wgts.control.eventbus.TaskDetailsEventBus;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerWgtsTaskLobbyFragmentComponent;
import com.wgts.wgts.dagger.module.WgtsTaskLobbyFragmentModule;
import com.wgts.wgts.mvp.presenter.IWgtsTaskLobbyFragmentPresenter;
import com.wgts.wgts.mvp.view.IWgtsTaskLobbyFragmentView;
import com.wgts.wgts.mvp.view.activity.login.LoginActivity;
import com.wgts.wgts.netmodle.resp.TaskListEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CustomImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/*
 *
 *   任务大厅
 *
 */public class WgtsWgtsTaskLobbyFragment extends LazyBaseFragment implements IWgtsTaskLobbyFragmentView {
    @Inject
    IWgtsTaskLobbyFragmentPresenter presenter;
    @BindView(R.id.linear_title)
    LinearLayout linearTitle;
    @BindView(R.id.vip_task_time)
    TextView vipTaskTime;
    @BindView(R.id.vip_task_recycle)
    RecyclerView vipTaskRecycle;
    @BindView(R.id.high_vip_task_time)
    TextView highVipTaskTime;
    @BindView(R.id.high_vip_task_recycle)
    RecyclerView highVipTaskRecycle;
    Unbinder unbinder;
    @BindView(R.id.my_join_task)
    TextView myJoinTask;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.task_rule_image)
    ImageView taskRuleImage;
    @BindView(R.id.vip_placeholder_ly)
    LinearLayout vipPlaceholderLy;
    @BindView(R.id.svip_placeholder_ly)
    LinearLayout svipPlaceholderLy;
    private TaskVipAdapter vipAdapter;
    private TaskHighVipAdapter highVipAdapter;
    private int vipCount, svipCount;
    private boolean vipTaskType = false, svipTaskType = false;
    private List<TaskListEntity.Task> vipTaskList, svipTaskList;
    private WonderLoadingDialog dialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wgts_task_lobby_fragment, viewGroup, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void init() {
        DaggerWgtsTaskLobbyFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .wgtsTaskLobbyFragmentModule(new WgtsTaskLobbyFragmentModule(this))
                .build()
                .inject(this);
    }

    private void initView() {
        EventBus.getDefault().register(this);
        CustomImageView.getInstance().customImage(getContext(), taskRuleImage, 1 / (double) 1);
        dialog = new WonderLoadingDialog(getActivity());
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadmore(false);
        refresh();
    }

    private void initVipRecycle(List<TaskListEntity.Task> list) {
        CustomLineaLayoutManager lineaLayoutManager = new CustomLineaLayoutManager(getContext());
        lineaLayoutManager.setScrollEnabled(false);
        vipTaskRecycle.setLayoutManager(lineaLayoutManager);
        vipAdapter = new TaskVipAdapter(getContext(), list);
        vipTaskRecycle.setAdapter(vipAdapter);
        vipAdapter.setOnClickListern(new TaskVipAdapter.onClickListern() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getContext(), WgtsTaskDetailsActivity.class);
                intent.putExtra(WgtsTaskDetailsActivity.TASK_TYPE, vipTaskType);
                intent.putExtra(WgtsTaskDetailsActivity.TASK_ID, vipTaskList.get(position).id);
                intent.putExtra(WgtsTaskDetailsActivity.COME_TYPE, WgtsTaskDetailsActivity.LOBBY);
                startActivity(intent);
            }
        });
    }

    private void initHighVipRecycle(List<TaskListEntity.Task> list) {
        CustomLineaLayoutManager layoutManager = new CustomLineaLayoutManager(getContext());
        layoutManager.setScrollEnabled(false);
        highVipTaskRecycle.setLayoutManager(layoutManager);
        highVipAdapter = new TaskHighVipAdapter(getContext(), list);
        highVipTaskRecycle.setAdapter(highVipAdapter);
        highVipAdapter.setOnClickListern(new TaskHighVipAdapter.onClickListern() {
            @Override
            public void onClick(View view, int position) {
                Intent intent = new Intent(getContext(), WgtsTaskDetailsActivity.class);
                intent.putExtra(WgtsTaskDetailsActivity.TASK_TYPE, svipTaskType);
                intent.putExtra(WgtsTaskDetailsActivity.TASK_ID, svipTaskList.get(position).id);
                intent.putExtra(WgtsTaskDetailsActivity.COME_TYPE, WgtsTaskDetailsActivity.LOBBY);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onError(int status, String msg) {
        refreshLayout.finishRefresh();
        MoHuToast.showToast(msg);
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    protected void loadData() {
        if (dialog != null) {
            dialog.show();
        }
        presenter.initList();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @OnClick(R.id.my_join_task)
    public void onViewClicked() {
        if (AppInfor.getInstance().getRefreshToken() != null && !AppInfor.getInstance().getRefreshToken().equals("")) {
            Intent intent = new Intent(getContext(), MyJoinTaskTaskActivity.class);
            startActivity(intent);
        } else {
            Intent intent2 = new Intent(getContext(), LoginActivity.class);
            startActivity(intent2);
        }
    }

    @Override
    public void initList(List<TaskListEntity> entities) {
        refreshLayout.finishRefresh();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        vipTaskList = entities.get(0).list;
        svipTaskList = entities.get(1).list;
        //任务列表占位图展示
        if (vipTaskList.size() > 0){
            vipPlaceholderLy.setVisibility(View.GONE);
        }else {
            vipPlaceholderLy.setVisibility(View.VISIBLE);
        }
        if (svipTaskList.size() > 0){
            svipPlaceholderLy.setVisibility(View.GONE);
        }else {
            svipPlaceholderLy.setVisibility(View.VISIBLE);
        }
        initVipRecycle(vipTaskList);
        initHighVipRecycle(svipTaskList);

        //展示可领取任务次数
        vipCount = entities.get(0).count;
        svipCount = entities.get(1).count;
        vipTaskTime.setText("今日可领任务次数(" + vipCount + "/" + entities.get(0).totalCount + ")");
        highVipTaskTime.setText("今日可领任务次数(" + svipCount + "/" + entities.get(1).totalCount + ")");
        //判断是否可以进行任务
        if (vipCount == entities.get(0).totalCount) {
            vipTaskType = false;
        } else {
            vipTaskType = true;
        }
        if (svipCount == entities.get(1).totalCount) {
            svipTaskType = false;
        } else {
            svipTaskType = true;
        }
    }

    private void refresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (dialog != null) {
                    dialog.show();
                }
                presenter.initList();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(TaskDetailsEventBus event) {
        presenter.initList();
    }
}
